﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class AB55DI : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        AB55DIData m_Data, m_DataX;
        public DataSet setUI = new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;

        #endregion
        public AB55DI()
        {
            InitializeComponent();
            this.Name = "织造单";

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = true;
            dicParamater["DocType"] = "WEAVINGORDER";
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new AB55DIData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }

        private void InitDataBinding()
        {

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                InitDataBinding();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        private void MainDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwABPOS = drv.Row;
                this.txtABPAUFKRS.Text = dwABPOS["ABPAUFKRS"].ToString();
                this.txtABPAUFLFD.Text = dwABPOS["ABPAUFLFD"].ToString();
                this.txtABPAUFPOS.Text = dwABPOS["ABPAUFPOS"].ToString();


                ReloadGAAUF(dwABPOS);
                string order = dwABPOS["ABPAUFKRS"].ToString() + dwABPOS["ABPAUFLFD"].ToString() + dwABPOS["ABPAUFPOS"].ToString();
                dwABPOS["REMARK"] = m_Data.GetCommentsFromTXTIND("ABP", order);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }


        private void ReloadGAAUF(DataRow dwABPOS)
        {
            setUI = m_Data.GetGAAUF(setUI, dwABPOS);
            this.listGAAUF.DataContext = setUI;
        }

        private void ReloadABFOLGE(DataRow dwABPOS)
        {
            setUI = m_Data.GetABFOLGE(setUI, dwABPOS);
            //this.DataContext = setUI;
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            DataRow dwTmp = dwABPOS.Table.Clone().Rows.Add();
            dwTmp.ItemArray = dwABPOS.ItemArray;
            DataSet setRpt = new DataSet();
            setRpt = m_Data.BuildAB55DIData(setRpt, dwABPOS);
            //dwABPOS.ItemArray = dwTmp.ItemArray;
            dicParamater["ABPAUFKRS"] = dwABPOS["ABPAUFKRS"].ToString();
            dicParamater["ABPAUFLFD"] = dwABPOS["ABPAUFLFD"].ToString();
            dicParamater["ABPAUFPOS"] = dwABPOS["ABPAUFPOS"].ToString();
            m_DataX = new AB55DIData(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            AB55DIReport m_rpt = new AB55DIReport(dicParamater);
            m_rpt.dsSource = setRpt;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.GetReport("EXCEL");
        }

        private void btnQuery1_Click(object sender, RoutedEventArgs e)
        {
            string KRS = txtAUFKRS.Text.Trim();
            string LFD = txtAUFLFD.Text.Trim();
            string POS = txtAUFPOS.Text.Trim();

            if (LFD.Trim().Length > 0)
            {
                LFD = m_Data.Right("000000" + LFD, 6);
            }

            if (POS.Trim().Length > 0)
            {
                POS = m_Data.Right("000" + POS, 3);
            }
            setUI = (DataSet)TryFindResource("setUI");
            if (setUI == null)
            {
                setUI = new DataSet();
            }
            this.setUI.Tables.Clear();
            setUI.AcceptChanges();
            DataTable tbUI = new DataTable("TableUI");
            tbUI.Columns.Add("KRS", typeof(string));
            tbUI.Columns.Add("LFD", typeof(string));
            tbUI.Columns.Add("POS", typeof(string));
            DataRow dwUI = tbUI.NewRow();

            dwUI["KRS"] = KRS;
            dwUI["LFD"] = LFD;
            dwUI["POS"] = POS;
            // DataRow dwTmp = dwABPOS.Table.Clone().Rows.Add();
            // dwTmp.ItemArray = dwABPOS.ItemArray;
            // dwABPOS["ABPAUFKRS"] = KRS;
            // dwABPOS["ABPAUFLFD"] = LFD;
            // dwABPOS["ABPAUFPOS"] = POS;
            DataSet setRpt = new DataSet();
            setRpt = m_Data.BuildSAMPLEORDERData(setRpt, dwUI);
            setUI.Clear();
            dicParamater["ABPAUFKRS"] = KRS;
            dicParamater["ABPAUFLFD"] = LFD;
            dicParamater["ABPAUFPOS"] = POS;

            //dwABPOS.ItemArray = dwTmp.ItemArray;            
            m_DataX = new AB55DIData(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            AB55DIReport m_rpt = new AB55DIReport(dicParamater);
            m_rpt.dsSource = setRpt;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.GetWeavingReport("EXCEL");
            txtAUFKRS.Text = "";
            txtAUFLFD.Text = "";
            txtAUFPOS.Text = "";
        }

        private void btnQuery2_Click(object sender, RoutedEventArgs e)
        {
            string KRS = txtAUFKRS.Text.Trim();
            string LFD = txtAUFLFD.Text.Trim();
            string POS = txtAUFPOS.Text.Trim();

            if (LFD.Trim().Length > 0)
            {
                LFD = m_Data.Right("000000" + LFD, 6);
            }

            if (POS.Trim().Length > 0)
            {
                POS = m_Data.Right("000" + POS, 3);
            }
            setUI = (DataSet)TryFindResource("setUI");
            if (setUI == null)
            {
                setUI = new DataSet();
            }
            this.setUI.Tables.Clear();
            setUI.AcceptChanges();
            DataTable tbUI = new DataTable("TableUI");
            tbUI.Columns.Add("KRS", typeof(string));
            tbUI.Columns.Add("LFD", typeof(string));
            tbUI.Columns.Add("POS", typeof(string));
            DataRow dwUI = tbUI.NewRow();

            dwUI["KRS"] = KRS;
            dwUI["LFD"] = LFD;
            dwUI["POS"] = POS;
            // DataRow dwTmp = dwABPOS.Table.Clone().Rows.Add();
            // dwTmp.ItemArray = dwABPOS.ItemArray;
            // dwABPOS["ABPAUFKRS"] = KRS;
            // dwABPOS["ABPAUFLFD"] = LFD;
            // dwABPOS["ABPAUFPOS"] = POS;
            DataSet setRpt = new DataSet();
            setRpt = m_Data.BuildTECHSAMPLEData(setRpt, dwUI);
            setUI.Clear();
            dicParamater["ABPAUFKRS"] = KRS;
            dicParamater["ABPAUFLFD"] = LFD;
            dicParamater["ABPAUFPOS"] = POS;

            //dwABPOS.ItemArray = dwTmp.ItemArray;            
            m_DataX = new AB55DIData(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            AB55DIReport m_rpt = new AB55DIReport(dicParamater);
            m_rpt.dsSource = setRpt;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.GetSAMPLEReport("EXCEL");
            txtAUFKRS.Text = "";
            txtAUFLFD.Text = "";
            txtAUFPOS.Text = "";
        }

        private void butGetData_Click(object sender, RoutedEventArgs e)
        {
            using (new LongOperation(this, "数据查询中"))
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT Distinct '' AS LSNAME1,A.ABPAUFKRS ,A.ABPAUFLFD,A.ABPAUFPOS,A.ABPART,A.ABPVAR, "
                    + " A.ABPFARBE,A.ABPSOLLMENGE1/100 AS WEI,SUM(C.ABASANZSTK*C.ABASSTKLG/1000) as QUANTITY, "
                    + " TRUNC(A.ABPSOLLMENGE1/100/D.WAKEAFAKTR*1000) AS QTY,A.ABPSTARTDAT,A.ABPENDDAT,'' AS REMARK  "
                    + " FROM ABPOS A,ABAUFM C,WASTKE D "
                    + " WHERE A.ABPMANDANT = D.WAKMANDANT AND A.ABPART = D.WAKART "
                    + " AND TRIM(A.ABPVAR) = TRIM(D.WAKVAR) AND A.ABPFARBE = D.WAKWCOL "
                    + " AND A.ABPMANDANT = C.ABAMANDANT AND A.ABPAUFKRS = C.ABAVKAUFKRS "
                    + " AND A.ABPAUFLFD = C.ABAVKAUFLFD AND A.ABPAUFPOS = C.ABAVKAUFPOS "
                    + " AND A.ABPAUFKRS LIKE 'W%'  AND A.ABPERLKZ <>'J'  "
                    + " GROUP BY A.ABPAUFKRS ,A.ABPAUFLFD,A.ABPAUFPOS,A.ABPART,A.ABPVAR,"
                    + " A.ABPFARBE,A.ABPSOLLMENGE1,A.ABPSTARTDAT,A.ABPENDDAT,D.WAKEAFAKTR "
                    // +" and abpaufkrs = 'WB' and abpauflfd = '000089' AND ABPERLKZ <>'J'  "
                 ;


                dsResult = m_Data.Connect2Proxy(orlReadCommand);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "ABPOS";

                    foreach (DataRow dwABPOS in dsResult.Tables["ABPOS"].Rows)
                    {
                        ReloadABFOLGE(dwABPOS);
                    } 
                }
            }
            setUI.Merge(dsResult);
            this.DataContext = setUI;
        }
    }
}
